package com.rs.game.player;

import java.io.Serializable;

import com.rs.game.item.Item;
import com.rs.game.item.ItemsContainer;
import com.rs.utils.Utils;

public final class Inventory implements Serializable {

	private static final long serialVersionUID = 8842800123753277093L;
	
	private ItemsContainer<Item> items;
	
	private transient Player player;
	
	public Inventory() {
		items = new ItemsContainer<Item>(28, false);
	}
	
	public void setPlayer(Player player) {
		this.player = player;
	}
	
	public void init() {
		player.getPackets().sendItems(149, 0, 93, items);
	}
	
	public void reset() {
		items.reset();
		init(); //as all slots reseted better just send all again
	}
	
	public void refresh(int... slots) {
		player.getPackets().sendUpdateItems(149, 0, 93, items, slots);
	}

	public boolean addItem(int itemId, int amount) {
		if(itemId < 0 || amount < 0 || itemId >= Utils.getItemDefinitionsSize())
			return false;
		Item[] itemsBefore = items.getItemsCopy();
		if(!items.add(new Item(itemId, amount))) {
			items.add(new Item(itemId, items.getFreeSlots()));
			player.getPackets().sendGameMessage("Not enough space in your inventory.");
			refreshItems(itemsBefore);
			return false;
		}
		refreshItems(itemsBefore);
		return true;
	}
	
	public void deleteItem(int slot, Item item) {
		Item[] itemsBefore = items.getItemsCopy();
		items.remove(slot, item);
		refreshItems(itemsBefore);
	}
	
	public void deleteItem(int itemId, int ammount) {
		Item[] itemsBefore = items.getItemsCopy();
		items.remove(new Item(itemId, ammount));
		refreshItems(itemsBefore);
	}
	
	/*
	 * No refresh needed its client to who does it :p
	 */
	public void switchItem(int fromSlot, int toSlot) {
		Item fromItem = items.get(fromSlot);
		Item toItem = items.get(toSlot);
		items.set(fromSlot, toItem);
		items.set(toSlot, fromItem);
	}
	
	public void refreshItems(Item[] itemsBefore) {
		int[] changedSlots = new int[itemsBefore.length];
		int count = 0;
		for(int index = 0; index < itemsBefore.length; index++) {
			if(itemsBefore[index] != items.getItems()[index])
				changedSlots[count++] = index;
		}
		int[] finalChangedSlots = new int[count];
		System.arraycopy(changedSlots, 0, finalChangedSlots, 0, count);
		refresh(finalChangedSlots);
	}
	
	public ItemsContainer<Item> getItems() {
		return items;
	}	
	
	public boolean hasFreeSlots() {
		return items.getFreeSlot() != -1;
	}
	
	public int getFreeSlots() {
		return items.getFreeSlots();
	}
	
	public Item getItem(int slot) {
		return items.get(slot);
	}
	
	public int getItemsContainerSize() {
		return items.getSize();
	}
	
	public boolean containsItem(int itemId, int ammount) {
		return items.contains(new Item(itemId, ammount));
	}

	public int getNumberOf(int itemId) {
		int number = 0;
		for(int i = 1; i < 20000000; i++) {
			if(containsItem(itemId, i)) {
				number++;
			} else {
				break;
			}
		}
		return number;
	}
	public boolean containsOneItem(int... itemIds) {
		for(int itemId : itemIds) {
			if(items.containsOne(new Item(itemId, 1)))
				return true;
		}
		return false;
	}
	
}
